package com.leslie.hhcommunity.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author
 * @PackageName:com.leslie.hhcommunity.controller.aspect
 * @ClassnName:ServiceLogAspect
 * @Description: AOP统一日志处理
 * @Date 2022/1/5  12:23
 */
@Component
@Aspect
public class ServiceLogAspect {
	private static final Logger LOGGER = LoggerFactory.getLogger(ServiceLogAspect.class);

	@Pointcut("execution(* com.leslie.hhcommunity.service.*.*(..))")
	public void pointcut() {
	}

	@Before("pointcut()")
	public void before(JoinPoint joinPoint) {
		// 用户[1.2.3.4],在[xxxx],访问了[tech.turl.community.service.xxx()].
		ServletRequestAttributes attributes =
				(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if (attributes == null) {
			return;
		}
		HttpServletRequest request = attributes.getRequest();
		String ip = request.getRemoteHost();
		String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		String target =
				joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
		LOGGER.info(String.format("用户[%s],在[%s],访问了[%s].", ip, now, target));
	}
}
